<?php


namespace App\Job;


use App\Config\CacheConfig;
use App\Config\QueueConfig;
use App\Http\Library\Lock;
use App\Http\Library\Pfs;
use App\Http\Library\Queue;
use App\Http\Model\Job\PfsHeadQueryAccountBalanceModel;
use App\Http\Model\Job\PfsHeadQueryTransferBillModel;

class PfsHeadQueryAccountBalanceJob
{

    /**
     * 3.8 监管户余额查询   机构端 -> 银行
     *
     * @param array $data
     *
     * @throws \Exception
     */
    public static function main(array $data)
    {
        $result = [];
        try {
            /*串行锁*/
            if (Lock::lock(CacheConfig::PFS_HEAD_QUERYACCOUNTBALANCE . ":{$data['pfs_agencyid']}", 0, 600) === false) {
                return 0;
            }
            /*3.8 监管户余额查询   机构端 -> 银行*/
            $result = Pfs::pfsHeadQueryAccountBalance([
                /*机构编号，由招行分配*/
                'agencyId' => $data['pfs_agencyid'],
            ], [
                /*招行-加密密钥*/
                'pfs_encrypt_private' => $data['pfs_encrypt_private'],
                /*招行-签名密钥*/
                'pfs_sign_private'    => $data['pfs_sign_private']
            ]);
            /*刷新账户余额*/
            PfsHeadQueryAccountBalanceModel::save($data, $result);
            /*
             * 释放锁
             * 只有正常处理的时候释放锁. 异常情况下不释放锁,等待锁过期释放
             * */
            Lock::unlock(CacheConfig::PFS_HEAD_QUERYACCOUNTBALANCE . ":{$data['pfs_agencyid']}");
        } catch (\Exception $exception) {
            log_arr([
                '[监管户余额查询-异常]',
                'line'   => $exception->getLine(),
                'file'   => $exception->getFile(),
                'msg'    => $exception->getMessage(),
                'data'   => serialize($data),
                'result' => $result,
            ], 'pfs');
            throw new \Exception($exception->getMessage());
        }
    }


    /**
     * 队列任务空闲回调
     * 3.8 监管户余额查询   机构端 -> 银行
     *
     * @throws \Exception
     */
    public static function call()
    {
        /*需要执行查询划拨的机构*/
        $list = PfsHeadQueryTransferBillModel::listPsfDw();
        foreach ($list as $val) {
            Queue::rpush([
                /*招行&数仓-业务线分校配置信息主键ID*/
                'pfs_dw_id'           => $val->id,
                /*机构编号，由招行分配*/
                'pfs_agencyid'        => $val->pfs_agencyid,
                /*招行-签名密钥*/
                'pfs_sign_private'    => $val->pfs_sign_private,
                /*招行-加密密钥*/
                'pfs_encrypt_private' => $val->pfs_encrypt_private,
                /*主体*/
                'ledger_subject_id'   => $val->ledger_subject_id,
                'ledger_subject_name' => $val->ledger_subject_name,
                /*城市*/
                'city_name'           => $val->city_name,
                'area_code'           => $val->area_code,
            ], QueueConfig::PFS_HEAD_QUERYACCOUNTBALANCE);
        }
    }

}





/**
 * PfsHeadQueryAccountBalanceJob.php
 *
 * 说明:
 *
 * 修改历史
 * ----------------------------------------
 * 2021/6/9 17:49:00   操作:创建
 **/
